package code.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import code.service.IUserService;
import code.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import code.mapper.UserMapper;
import code.model.User;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User add(User user) {
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return user;
        }
        return null;
    }

    @Override
    public String delete(String id) {
        int delete = userMapper.deleteById(id);
        if (delete > 0) {
            return id;
        }
        return null;
    }

    @Override
    public User edit(User edit) {
        int update = userMapper.updateById(edit);
        if (update > 0) {
            return edit;
        }
        return null;
    }

    @Override
    public User findOne(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public PageVo<User> findPage(String userName, Integer page, Integer pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(userName)) {
            queryWrapper.like("name", userName);
        }
        IPage<User> pageParam = new Page<>(page, pageSize);
        IPage<User> userIPage = userMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(userIPage);
    }

    @Override
    public List<User> listByIds(List<String> userIds) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",userIds);
        return userMapper.selectList(queryWrapper);
    }
}
